
#you need to modify it for you
DEST_IP_ADDRESS = 'localhost'
DEST_IP_PORT    = 8001
THREAD_NUMBER   = 1

#end_string used for tag the xml file was ending
XML_END_STRING = '</Eadp>'

#file used for transimit and test
SUBSCRIBE_XML_BAD_USER_REQ = './xml/bad_subscribe_req.xml'    
SUBSCRIBE_XML_USER_REQ     = './xml/subscribe_req.xml'    
UNSUBSCRIBE_XML_REQ        = './xml/un_subscribe_req.xml'
DATA_REQUEST_GPS_XML_REQ   = './xml/DataRequest_GPS.xml'

#received xml file will saved in a format
#xml+thread_id
SUBSCRIBE_XML_RES          = './test/Subscribe_Resp.xml'
DATA_RES_XML_RES           = './test/DataRequestResp_GPS.xml'
#xml+thread_id+seqnumber
DATA_ASSIST_XML_RES        = './test/GPS_AssistData.xml'


import string, threading, time
import sys
#send the file_in file out from socket line by line
def send_xml_file(socket, file_in):
    file_handle = open ( file_in )  
    fileList = file_handle.readlines()
    for fileLine in fileList:  
        print '>>', fileLine  
        socket.send(fileLine)
    file_handle.close()

#received an xml file and saved in file_out , end_string used for terminal the file
def received_xml_file(socket, file_out, end_string):
    buf = ''
    while True:
        tempbuf = socket.recv(4096)
        if not tempbuf:
            return tempbuf
        buf += tempbuf
        found = buf.find(end_string)
        if found:  
            file_handle = open ( file_out ,'w')  
            file_handle.write(buf)
            file_handle.close()
            return buf
    

#do socket_req_res one time
def do_socket_req_res_(socket, file_in, file_out, end_string):
    send_xml_file(socket, file_in)
    received_xml_file(socket,file_out,end_string)
    

class delivery_behavior:
    ads_period          = 0
    ads_onchange        = 1
    ads_once            = 2
    ads_stop            = 3
    ads_unsupported     = 4

def take_care_long_time_connection(socket, thread_id):
    file_res = '%s.0x%x'%(SUBSCRIBE_XML_RES,thread_id)
    #subscribe_req
    send_xml_file(socket, SUBSCRIBE_XML_USER_REQ)
    #get subscribe_res
    received_xml_file(socket,file_res,XML_END_STRING)
    #send data req
    send_xml_file(socket, DATA_REQUEST_GPS_XML_REQ)
    
    #get data res
    file_res = '%s.0x%x'%(DATA_RES_XML_RES,thread_id)
    received_xml_file(socket,file_res,XML_END_STRING)
    i = 0
    while True:
        i = i+1
        file_res = '%s.0x%x.seq.%d'%(DATA_ASSIST_XML_RES,thread_id, i)
        #recv data assist data
        buf = received_xml_file(socket,file_res,XML_END_STRING)
        if not buf:
            print 'socket closed!'
            break;
        print file_res+' saved!'

    
def create_socket_do_business(nums):
    
    import socket 
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error as msg:
        s = None

    try:
        s.connect((DEST_IP_ADDRESS, DEST_IP_PORT))  
    except socket.error as msg:
        s.close()
        s = None

    if s is None:
        print 'could not open socket'
        sys.exit(1)
    take_care_long_time_connection(s, threading.currentThread().ident)
    s.close()

    
def main(num):
    global count, mutex
    threads = []
 
    count = 1
    mutex = threading.Lock()
    for x in xrange(0, num):
        threads.append(threading.Thread(target=create_socket_do_business, args=(10,)))
    for t in threads:
        t.start()
    for t in threads:
        t.join()  
 
 
if __name__ == '__main__':
    main(THREAD_NUMBER)